<?php

namespace app\common\service;

use think\facade\Cache;
use think\facade\Db;

/**
 * Class UserService
 * @package app\common\service
 */
class UserService
{
    // VIP相关
    const TYPE_EXT_VIP_ID = 1; // 用户开通的VIP等级id
    const TYPE_EXT_VIP_EXPIRE = 2; // 用户VIP过期时间戳
    const TYPE_EXT_VIP_START = 3; // 用户开通VIP时间
    // 统计类
    const TYPE_EXT_TOTAL_STUDY_TIME = 100; // 学习时间总计
    const TYPE_EXT_TOTAL_STUDY_COMPLETED = 101; // 完成学习计划数量

    /**
     * @param $userId
     * @param $type
     * @return mixed|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getExtValue($userId, $type)
    {
        if ($value = Cache::get("UserExt:{$userId}:{$type}")) {
            return $value;
        }
        $userExt = Db::table('user_ext')->where(['uid' => $userId, 'type' => $type])->find();
        if ($userExt) {
            return $userExt['value'];
        } else {
            return null;
        }
    }

    /**
     * @param $userId
     * @param $type
     * @param $value
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function setExtValue($userId, $type, $value)
    {
        $userExt = Db::table('user_ext')->where(['uid' => $userId, 'type' => $type])->find();
        if (!$userExt) {
            Db::table('user_ext')->insert([
                'uid' => $userId,
                'type' => $type,
                'value' => $value,
                'update_time' => date('Y-m-d H:i:s')
            ]);
        } else {
            Db::table('user_ext')
                ->where([
                    'uid' => $userId,
                    'type' => $type
                ])
                ->data([
                    'value' => $value,
                    'update_time' => date('Y-m-d H:i:s')
                ])
                ->save();
        }
        Cache::set("UserExt:{$userId}:{$type}", $value, 3600);
    }

    /**
     * @param $userId
     * @param $type
     * @param int $incr
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function setExtValueIncr($userId, $type, $incr = 1)
    {
        $num = (float)self::getExtValue($userId, $type);
        self::setExtValue($userId, $type, $num + $incr);
    }

    /**
     * 获取用户vip信息，如果未开通或已过期则返回null
     * @param $userId
     * @return array|null
     */
    public static function getVipInfo($userId)
    {
        $info = Db::table('user_ext')
            ->where('uid', $userId)
            ->where('type', 'in', [self::TYPE_EXT_VIP_ID, self::TYPE_EXT_VIP_EXPIRE, self::TYPE_EXT_VIP_START])
            ->column('type,value', 'type');
        if ($info && !empty($info[self::TYPE_EXT_VIP_ID]['value']) && (strtotime($info[self::TYPE_EXT_VIP_EXPIRE]['value'])) > time()) {
            return [
                'id' => $info[self::TYPE_EXT_VIP_ID]['value'],
                'expire' => $info[self::TYPE_EXT_VIP_EXPIRE]['value'],
                'start' => $info[self::TYPE_EXT_VIP_START]['value']
            ];
        }
        return null;
    }
}